//
//  taskHeaderView.swift
//  demo01
//
//  Created by babybus on 2025/7/1.
//
import Foundation
import UIKit
import SnapKit
import Alamofire

class TaskHeaderView:UICollectionReusableView{
    //全局变量
    var describeLable:doubleLabel
    public var finalTime:String = "2025.07.11"
    var remainingNumber:String = "1"
    var headerButton:MyButton
    let api = "/tasklist-header"
    //初始化方法
    override init(frame: CGRect) {
        describeLable = doubleLabel(mainLabel: UILabel(),secondLabel: UILabel())
        headerButton = MyButton()
        super.init(frame: frame)
        loadData(userId: 1)
//        setupUI()

    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func attributeInit(){}
    func setupUI(){
        
        //设置标签
        describeLable.mainLabel.text = "精品资源解锁次数"+finalTime+"到期"
        describeLable.secondLabel.text = "解锁次数剩余：" + remainingNumber + "次"
        describeLable.mainLabel.font = UIFont.systemFont(ofSize: 14,weight: .bold)
        describeLable.secondLabel.font = UIFont.systemFont(ofSize: 13,weight: .light)
        describeLable.mainLabel.adjustsFontSizeToFitWidth = true // 开启自动缩放
        describeLable.mainLabel.minimumScaleFactor = 0.5 // 最小缩放比例（默认0.5）
        //  标签部分设置颜色
        describeLable.mainLabel.attributedText = ColorTool.setColorForPartText(start: 8,
                                                                               length: finalTime.count+2,
                                                                                targetText: describeLable.mainLabel.text!,
                                                                                color: .orange)
        describeLable.secondLabel.attributedText = ColorTool.setColorForPartText(start: 7,
                                                                                 length: remainingNumber.count+1,
                                                                                targetText: describeLable.secondLabel.text!,
                                                                                color: .orange)
        //设置按钮
//        let hsbGold = UIColor(red: 0.95, green: 0.80, blue: 0.30, alpha: 1.0)
//        headerButton.configure(title: "", titleFor: .normal, titleColor: .brown, titleColorFor: .normal, backgroundColor: hsbGold)
        headerButton.layer.cornerRadius = 15
        headerButton.setImage(UIImage(named: "./upStrategy"), for: .normal)
        
        //将标签上下放置到stack
        let leftStack = UIStackView()
        leftStack.axis = .vertical
        leftStack.distribution = .fill
        leftStack.alignment = .fill
        leftStack.spacing = 5
        leftStack.addArrangedSubview(describeLable.mainLabel)
        leftStack.addArrangedSubview(describeLable.secondLabel)
        
        //将（标签）stack 和按钮放入header
        let containerStack = UIStackView()
        containerStack.axis = .horizontal
        containerStack.distribution = .fill
        containerStack.alignment = .center
        containerStack.spacing = 10
        containerStack.addArrangedSubview(leftStack)
        containerStack.addArrangedSubview(headerButton)
        
        self.addSubview(containerStack)
        containerStack.snp.makeConstraints { make in
            make.edges.equalToSuperview().inset(UIEdgeInsets(top: 10, left: 15, bottom: 10, right: 15))
        }
        headerButton.snp.makeConstraints { make in
            make.width.equalTo(90)
            make.centerY.equalToSuperview()
        }
    }
    func loadData(userId:Int){
        let url = AllUrl.LH_URL + api + "?userId=\(userId)"
        
        AF.request(url,method: .get)
            .validate(statusCode: 200..<300)
            .responseDecodable(of:TaskListHeader.self){ response in
                switch response.result{
                case .success(let header):
                    DispatchQueue.main.async {
                        self.finalTime = header.finalTime
                        self.remainingNumber = header.remainingNumber
                        self.setupUI()
                    }
                case .failure(let error):
                    print("错误: \(error.localizedDescription)")
                }
            }
    }
}
